import { otherHttp } from '/@/utils/http/axios';
import {
  ShopUserParams,
  SingleUserParams,
  GetShopUserModel,
  GetSingleUserModel,
  AddShopUserParams,
  UpdateUserState,
  UpdateUserRole,
} from './model/shopUserModel';
import { ShopCommonResult } from './model/common';

import { ErrorMessageMode } from '/#/axios';

enum Api {
  users = '/users',
}

/**
 * @description: shop users api
 */
export function shopUsersApi(params: ShopUserParams, mode: ErrorMessageMode = 'message') {
  return otherHttp.get<GetShopUserModel>(
    {
      url: Api.users,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: add shop users api
 */
export function addShopUsers(params: AddShopUserParams, mode: ErrorMessageMode = 'message') {
  return otherHttp.post<ShopCommonResult>(
    {
      url: Api.users,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: update shop users api
 */
export function updateUserState(params: UpdateUserState, mode: ErrorMessageMode = 'message') {
  const { uId, type } = params;
  return otherHttp.put<ShopCommonResult>(
    {
      url: Api.users + `/${uId}/state/${type}`,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: delete shop users api
 */
export function deleteSingleUser(params: SingleUserParams, mode: ErrorMessageMode = 'message') {
  const { id } = params;
  return otherHttp.delete<ShopCommonResult>(
    {
      url: Api.users + `/${id}`,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: single users api
 */
export function getSingleUser(id: number, mode: ErrorMessageMode = 'message') {
  return otherHttp.get<GetSingleUserModel>(
    {
      url: Api.users + `/${id}/`,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: edit single users api
 */
export function putSingleUser(params: SingleUserParams, mode: ErrorMessageMode = 'message') {
  const { id, email, mobile } = params;
  return otherHttp.put<GetSingleUserModel>(
    {
      url: Api.users + `/${id}/`,
      params: {
        email,
        mobile,
      },
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: edit single users api
 */
export function putUserRole(params: UpdateUserRole, mode: ErrorMessageMode = 'message') {
  const { id, rid } = params;
  return otherHttp.put<GetSingleUserModel>(
    {
      url: Api.users + `/${id}/` + 'role',
      params: {
        rid,
      },
    },
    {
      errorMessageMode: mode,
    },
  );
}
